package com.zhanqiao.controller;

import com.google.gson.Gson;
import com.zhanqiao.domain.ArticleBean;
import com.zhanqiao.domain.ResponseBean;
import com.zhanqiao.service.SelectAllContentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/selectallcontent.action")
public class SelectAllContentController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String page = req.getParameter("page");
        String limit =  req.getParameter("limit");
        SelectAllContentService selectAllContentService = new SelectAllContentService();
        List<ArticleBean> list = selectAllContentService.articleService(page,limit);

        Map message = new HashMap();
        if (list != null) {
            ResponseBean responseBean = new ResponseBean();
            responseBean.setCode(0);
            responseBean.setMsg("");
            responseBean.setCount(1000);
            responseBean.setData(list);
            String result = new Gson().toJson(responseBean);
            System.out.println(result);
            resp.getWriter().write(result);
        } else {
            message.put("msg", "查询失败");
            String result = new Gson().toJson(message);
            resp.getWriter().write(result);
        }

    }
}
